package vip.kqsy.cloud.ai.server.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HtmlUtil {

    /**
     * 转换为html能识别的文本
     */
    public static String convert(String text) {
        Pattern pattern = Pattern.compile("```");
        Matcher matcher = pattern.matcher(text);
        Pattern pattern1 = Pattern.compile("```(\\w+)\n");

        int count = 0;
        while (matcher.find()) {
            if (count % 2 == 0) {
                String lang = "";
                Matcher matcher1 = pattern1.matcher(text);
                if (matcher1.find()) {
                    lang = matcher1.group(1);
                }
                text = text.replaceFirst("```.*", String.format("<pre><code class=\"language-%s\">", lang));
            } else {
                text = text.replaceFirst("```", "</code></pre>");
            }
            count++;
        }
        return text.replaceAll("\n", "<br>");
    }
}
